package com.ycsj.goldmedalnewconcept.teacher.adapter;

import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.base.CommonAdapter;
import com.ycsj.common.base.ViewHolder;
import com.ycsj.common.utils.ScreenUtils;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.teacher.activity.SearchByConditionActivity;
import com.ycsj.goldmedalnewconcept.teacher.bean.SearchInfo;
import com.ycsj.goldmedalnewconcept.teacher.bean.SearchItem;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 展示搜索条件的选项的列表适配器
 * Created by Administrator on 2019/2/22 0022.
 */

public class SearchGvAdapter extends CommonAdapter<SearchItem> {

    private RelativeLayout.LayoutParams lp;
    //当前条目的数据集合
    private List<SearchItem> datas;
    //整个外层列表的数据集合
    private List<SearchInfo> outDatas;

    //点击的外层位置
    private int outPos;

    public SearchGvAdapter(Context context, List<SearchItem> datas, int layoutId, List<SearchInfo> outDatas, int outPos) {
        super(context, datas, layoutId);
        int width = (ScreenUtils.getScreenWidth(context) - UiUtils.dp2px(32 + 52 + 8)) / 4;
        int height = width * 2 / 5;
        lp = new RelativeLayout.LayoutParams(width, height);
        this.datas = datas;
        this.outDatas = outDatas;
        this.outPos = outPos;
    }

    @Override
    public void convert(ViewHolder holder, final SearchItem searchItem) {
        TextView tv = holder.getView(R.id.tv);
        tv.setLayoutParams(lp);
        tv.setText(searchItem.name);
        if (searchItem.isChoosed) {
            tv.setBackgroundResource(R.drawable.tv_option_select);
        } else {
            tv.setBackgroundResource(R.drawable.tv_option_normal);
        }

        final int pos = holder.getPosition();
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (outPos == outDatas.size() - 1 || outPos == outDatas.size() - 2 || outPos == outDatas.size() - 3 || outPos == outDatas.size() - 4) {
                    //点击的是单元,先判断年级时候有选中的元素,如果年级有选中,单元不能选择
                    if (gradeHasChoosed()) {
                        //年级有选中时,单元不让选择
                        ToastUtils.showShort("单元与小升初,中考不能同时选择");
                    } else {
                        //年级没有选中时,单元可以选择
                        //点击的外层位置是1-1,1-2,1-3,1-4
//                        if (datas.get(pos).isChoosed) {
//                            //如果当前位置是选中的话,再次点击的时候取消
//                            datas.get(pos).isChoosed = !datas.get(pos).isChoosed;
//                            notifyDataSetChanged();
//                        } else {
//                            //当前位置没有选中的状态,在判断是否有单元选中,没有则将该条目选中
//                            if (isChooseUnit()) {
//                                //1-4  1-3 1-2 1-1 做单选处理判断
//                               ToastUtils.showShort("只能选择一个单元");
//                            } else {
//                                //选中的取消
//                                datas.get(pos).isChoosed = !datas.get(pos).isChoosed;
//                                notifyDataSetChanged();
//                            }
//                        }

                        if (!datas.get(pos).isChoosed) {
                            //选中的回调接口
                            if (onUnitChooseListener != null) {
                                onUnitChooseListener.onUnitChoosed(searchItem);
                            }
                        } else {
                            searchItem.isChoosed = !searchItem.isChoosed;
                            notifyDataSetChanged();
                        }

                        //通知知识点刷新
                        sendUpdataZsd();

                    }

                } else if (outPos == 0) {
                    //点击年级时,判断是否有单元选中,如果有单元选中,则年级不让选择,在单元没有选中的前提下,年级只能单选
                    if ("小升初".equals(searchItem.name) || "中考".equals(searchItem.name)) {
                        //点击的是小升初,中考

                        //判断年级是否有选中
                        if (isChooseUnit()) {
                            //有选中单元
                            ToastUtils.showShort("小升初,中考不能与单元同时选择");
                        } else {
                            //没有选中单元
                            changeGradeItemsState(searchItem);
                            //通知知识点刷新
                            sendUpdataZsd();
                        }

                    } else {
                        //点击的不是小升初和中考
                        changeGradeItemsState(searchItem);
                        //通知知识点刷新
                        sendUpdataZsd();
                    }

                } else {
                    //其它位置语言知识目标,难度是可以多选的
                    datas.get(pos).isChoosed = !datas.get(pos).isChoosed;
                    notifyDataSetChanged();
                    //通知知识点刷新
                    sendUpdataZsd();
                }
            }
        });
    }


    /**
     * 通知页面知识点刷新的事件
     */
    private void sendUpdataZsd() {
        BusInfo info = new BusInfo();
        info.type = SearchByConditionActivity.UPDATEZSD;
        EventBus.getDefault().post(info);
    }


    /**
     * 单元选择的回调接口
     */
    public interface OnUnitChooseListener {
        void onUnitChoosed(SearchItem searchItem);
    }

    public OnUnitChooseListener onUnitChooseListener;

    public void setOnUnitChooseListener(OnUnitChooseListener onUnitChooseListener) {
        this.onUnitChooseListener = onUnitChooseListener;
    }

    /**
     * 年级里所有标签单选的方法
     */
    private void changeGradeItemsState(SearchItem searchItem) {
        List<SearchItem> gradeItems = outDatas.get(0).searachItems;
        if (!searchItem.isChoosed) {
            for (int i = 0; i < gradeItems.size(); i++) {
                if (gradeItems.get(i) == searchItem) {
                    gradeItems.get(i).isChoosed = true;
                } else {
                    gradeItems.get(i).isChoosed = false;
                }
            }
        } else {
            //如果当前位置是选中的,点击取消
            searchItem.isChoosed = !searchItem.isChoosed;
        }
        notifyDataSetChanged();
    }

    /**
     * 判断是否有单元被选中,这里单元只能单选
     *
     * @return
     */
    private boolean isChooseUnit() {
        boolean res = false;
        //1-4 ,1-3,1-2,1-2
        List<SearchItem> unit1 = outDatas.get(outDatas.size() - 1).searachItems;
        List<SearchItem> unit2 = outDatas.get(outDatas.size() - 2).searachItems;
        List<SearchItem> unit3 = outDatas.get(outDatas.size() - 3).searachItems;
        List<SearchItem> unit4 = outDatas.get(outDatas.size() - 4).searachItems;

        List<SearchItem> allUnit = new ArrayList<>();
        allUnit.addAll(unit1);
        allUnit.addAll(unit2);
        allUnit.addAll(unit3);
        allUnit.addAll(unit4);

        for (int i = 0; i < allUnit.size(); i++) {
            if (allUnit.get(i).isChoosed) {
                res = true;
                break;
            }
        }
        return res;
    }


    /**
     * 判断年级里小升初和单元是否选中的方法
     */

    public boolean gradeHasChoosed() {
        boolean res = false;
        List<SearchItem> gradeItems = outDatas.get(0).searachItems;
        for (int i = 0; i < gradeItems.size(); i++) {
            if ("小升初".equals(gradeItems.get(i).name) || "中考".equals(gradeItems.get(i).name)) {
                if (gradeItems.get(i).isChoosed) {
                    res = true;
                }
            }
        }
        return res;
    }
}
